package com.manong.config;

import com.manong.web.intercptor.CheckLoginIntercptor;
import com.manong.web.intercptor.CheckPermissionIntercptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    private CheckLoginIntercptor checkLoginIntercptor;

    @Autowired
    private CheckPermissionIntercptor checkPermissionIntercptor;

    /**
     * 配置跨域请求
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
//                .allowedOrigins("*")
                .allowedOrigins("*") // 设置允许跨域请求的域名
                .allowedHeaders("userId", "Authorization", "Content-Type", "*") // 明确指定允许的请求头
                .allowCredentials(true) // 是否允许证书
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 允许的方法
                .maxAge(3600); // 跨域允许时间
    }

    /**
     * 把自己的拦截器交给mvc管理
     * 配置拦截器规则
     * 配置排除规则
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(checkLoginIntercptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/api/code", "/api/login", "/api/logout", "/api/menu/allMenu", "/api/menu/getMenuRoleById", "/api/permission/load");
        registry.addInterceptor(checkPermissionIntercptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/api/code", "/api/login", "/api/logout", "/api/menu/allMenu", "/api/menu/getMenuRoleById", "/api/permission/load");
    }
}
